#! /bin/sh

srcdir=$1

# Values from c-auto.h

mk_GREY=
mk_PS_GS=
mk_GS_PATH='gs'
mk_PS_DPS=
mk_PS_NEWS=
mk_A4=
mk_MOTIF=
mk_COLOR=

# T1 is always true
mk_T1=1

# Kpathsea defaults, they are #if'ed in the source but there is no
# config option to disable them.
mk_KPATHSEA=1

# Optional values from CFLAGS

mk_MFMODE='unspecified'
mk_BDPI=600
mk_DEFAULT_FONT_SIZES='m0:m0.5:m1:m2:m3:m4:m5'
mk_SHRINK=8

# Get arguments from CFLAGS

for mk_arg
do
  case "$mk_arg" in
    -D*=*)
      eval "`echo \"$mk_arg\" | sed 's/^-D/mk_/'`"
      ;;
    -D*)
      eval "`echo \"$mk_arg\" | sed 's/^-D/mk_/'`=1"
      ;;
  esac
done

# Get arguments from c-auto.h

eval "`awk '/^#define/ { print "mk_"$2"="$3 }' c-auto.h`"

# eval "`cat c-auto.h \
#   | grep '^[ 	]*#[ 	]*define[ 	]' \
#   | sed 's/^[ 	]*#[ 	]*define[ 	][ 	]*/mk_/' \
#   | sed 's/[ 	][ 	]*/=/'`"

# Process simple boolean arguments.  Their values are empty or nonempty

if test -z "$mk_COLOR";		then echo '/^#ifcolor/,/^#/d';		fi
if test -z "$mk_GREY";		then echo '/^#ifgrey/,/^#/d';		fi
if test -z "$mk_PS_DPS";	then echo '/^#ifdps/,/^#/d';		fi
if test -z "$mk_PS_NEWS";	then echo '/^#ifnews/,/^#/d';		fi
if test -z "$mk_T1";		then echo '/^#ift1/,/^#/d';		fi
if test -z "$mk_MOTIF";		then echo '/^#ifmotif/,/^#/d';
				else echo '/^#ifxaw/,/^#/d';		fi

if test -z "$mk_KPATHSEA"; then	echo	'/^#ifkpathsea/,/^#/d';
			   else echo	'/^#ifnokpathsea/,/^#/d';	fi

# Process remaining PostScript emulation arguments

if test -z "$mk_PS_GS"; then
  echo '/^#ifghost/,/^#/d'
else
  echo "s^@GS_PATH@^$mk_GS_PATH^"
fi

if test -n "$mk_PS_GS" || test -n "$mk_PS_DPS" || test -n "$mk_PS_NEWS"; then
  mk_PS=y
  echo '/^#ifnops/,/^#/d'
else
  mk_PS=
  echo '/^#ifps/,/^#/d'
fi

# get version info and release date from CHANGES file

spacetab=' 	'
XDVI_VERSION_INFO=`egrep "^[$spacetab]+\*" $srcdir/CHANGES | sed 1q | awk '{print $2 " " $3}'`
XDVI_VERSION=`echo $XDVI_VERSION_INFO | awk '{print $1}'`
XDVI_DATE=`echo $XDVI_VERSION_INFO | awk '{print $2}' | sed 's/[():]//g'`

# Miscellaneous args

if test -n "$mk_A4"; then
  echo 's/@DEFAULT_PAGE_SIZE@/21 x 29.7 cm (A4 size)/'
  echo 's/@DEFAULT_UNIT@/cm " (centimeters)"/'
else
  echo 's/@DEFAULT_PAGE_SIZE@/8.5 x 11 inches/'
  echo 's/@DEFAULT_UNIT@/in " (inches)"/'
fi

echo "s^@MFMODE@^$mk_MFMODE^"
echo "s/@BDPI@/$mk_BDPI/"
echo "s/@SHRINK@/$mk_SHRINK/"
echo "s/@XDVI_VERSION@/$XDVI_VERSION/"
echo "s^@XDVI_DATE@^$XDVI_DATE^"

echo "s/@DEFAULT_FONT_SIZES@/$mk_DEFAULT_FONT_SIZES/" | sed -e "s/:/ :\\\\\\
/g"

# Remove all remaining '#' lines

echo	'/^#/d'

# Done

exit 0
